﻿package {
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.utils.Timer;

	public class Game extends Sprite {
		static public var BINGO:Event=new Event("bingo");
		static public var WASH_ERR:Event=new Event("wash_err");
		static public var NO_TIME:Event=new Event("no_time");
		static public var NO_WASH:Event=new Event("no_wash");
		static public var GETGIFT:Event=new Event("getgift");
		static public var ENDTREE:Event=new Event("endtree");
		private var llkLevel:LLK;
		private var gift:Gift;
		private var losetxt:loseTXT;
		private var levelTxt:LevelTxt;
		private var theend:TheEnd;
		private var iconLeft:int;
		private var timeout:Number;
		private var _score:uint;
		private var _washTimes:int;
		private var _hintTimes:int;
		private var currentLevel:uint;
		private var levelArray:Array;

		private var hintBtn:llkBtn;
		private var randomBtn:llkBtn;
		private var resetBtn:llkBtn;
		private var scoreBtn:llkBtn;
		private var clockBar:TimeCount;
		private var llkTimer:Timer;
		private var cntdownTimer:Timer;
		private var bingomp3:bingoMP3;
		private var hintmp3:hintMP3;
		private var randommp3:randomMP3;
		private var dump3:duMP3;
		private var xmasmp3:XmasSound;

		private function get score():uint {
			return _score;
		}
		private function set score(n:uint):void {
			_score=n;
			scoreBtn.text2=n;
		}
		private function get washTimes():int {
			return _washTimes;
		}
		private function set washTimes(n:int):void {
			_washTimes=n;
			randomBtn.text2=n;
		}
		private function get hintTimes():int {
			return _hintTimes;
		}
		private function set hintTimes(n:int):void {
			_hintTimes=n;
			hintBtn.text2=n;
		}

		public function Game():void {
			addChilds();
			timeout=60;
			score=0;
			hintTimes=3;
			washTimes=2;
			currentLevel=0;
			levelArray=new Array();
			var i:uint=0;
			levelArray[i++]={xIcons:3,yIcons:2,iconTypes:3,lineWidth:60,llkWidth:48};
			levelArray[i++]={xIcons:4,yIcons:3,iconTypes:3,lineWidth:60,llkWidth:48};
			levelArray[i++]={xIcons:5,yIcons:4,iconTypes:5,lineWidth:54,llkWidth:40};
			levelArray[i++]={xIcons:7,yIcons:4,iconTypes:7,lineWidth:50,llkWidth:36};
			levelArray[i++]={xIcons:8,yIcons:5,iconTypes:10,lineWidth:45,llkWidth:32};
			levelArray[i++]={xIcons:12,yIcons:7,iconTypes:21,lineWidth:41,llkWidth:32};
			levelArray[i++]={xIcons:12,yIcons:7,iconTypes:21,lineWidth:41,llkWidth:32,changeStyle:"Down"};
			levelArray[i++]={xIcons:12,yIcons:7,iconTypes:21,lineWidth:41,llkWidth:32,changeStyle:"LeftRightOut"};
			levelArray[i++]={xIcons:12,yIcons:7,iconTypes:21,lineWidth:41,llkWidth:32,changeStyle:"UpDownIn"};
			levelArray[i++]={xIcons:12,yIcons:7,iconTypes:21,lineWidth:41,llkWidth:32,changeStyle:"4SideIn"};
			this.addEventListener(Game.NO_TIME.type,LOSE);
			this.addEventListener(Game.WASH_ERR.type,LOSE);
			this.addEventListener(Game.NO_WASH.type,LOSE);
		}
		public function start():void {
			initTimer();
			initLevel();
		}
		private function addChilds():void {

			resetBtn=new llkBtn(50,320,false,true,26,80);
			//addChild(resetBtn);
			hintBtn=new llkBtn(40,20,false,true,20,70);
			addChild(hintBtn);
			randomBtn=new llkBtn(110,20,false,true,20,70);
			addChild(randomBtn);
			resetBtn.addEventListener(MouseEvent.CLICK,resetBtnClick);
			scoreBtn=new llkBtn(250,20,false);
			addChild(scoreBtn);
			resetBtn.text="重新开始";
			hintBtn.text="提示";
			randomBtn.text="洗牌";
			scoreBtn.text="SCORE ";
			clockBar=new TimeCount  ;
			addChild(clockBar);
			clockBar.width=100;
			clockBar.height=100;
			clockBar.x=450;
			clockBar.y=15;
			losetxt=new loseTXT();
			levelTxt=new LevelTxt();
			bingomp3=new bingoMP3();
			randommp3=new randomMP3();
			hintmp3=new hintMP3();
			dump3=new duMP3();
			//endmp3=new WeWishYouAMerryChristmas;
		}
		private function initLevel():void {
			llkLevel=new LLK(levelArray[currentLevel],bingo);
			clockBar.stop();
			clockBar.gotoAndStop(1);
			cntdownTimer.stop();
			iconLeft=levelArray[currentLevel].xIcons*levelArray[currentLevel].yIcons;
			levelTxt.gotoAndStop(currentLevel+1);
			levelTxt.x=550/2;
			levelTxt.y=170;
			addChild(levelTxt);
			var levelTimer=new Timer(1500,1);
			levelTimer.addEventListener(TimerEvent.TIMER_COMPLETE,addLevel);
			levelTimer.start();
		}
		private function addLevel(e:TimerEvent):void {
			removeChild(levelTxt);
			addChild(llkLevel);
			hintBtn.addEventListener(MouseEvent.CLICK,hintBtnClick);
			randomBtn.addEventListener(MouseEvent.CLICK,randomBtnClick);
			llkTimer.reset();
			llkTimer.start();
		}
		private function initTimer():void {
			var repeat:int=clockBar.totalFrames;
			var interval:int=timeout*1000/repeat;
			llkTimer=new Timer(interval);
			llkTimer.addEventListener(TimerEvent.TIMER,timerHandler);
			cntdownTimer=new Timer(1);
			cntdownTimer.addEventListener(TimerEvent.TIMER,addTime);
		}
		private function randomBtnClick(e:MouseEvent=null) {
			if (washTimes>0) {
				if (llkLevel.randomBtnClick()) {
					washTimes--;
					randommp3.play();
				}

			}
		}
		private function hintBtnClick(e:MouseEvent=null) {
			if (hintTimes>0) {
				llkLevel.hintBtnClick(true);
				hintTimes--;
				hintmp3.play();
			}
		}
		private function resetBtnClick(e:MouseEvent) {
			resetBtn.x=50;
			resetBtn.y=320;
			removeChild(resetBtn);
			if (losetxt.stage) {
				removeChild(losetxt);
			}
			if (llkLevel!=null&&llkLevel.stage) {
				removeChild(llkLevel);
			}
			if (gift!=null&&gift.stage) {
				removeChild(gift);
			}
			if (theend!=null&&theend.stage) {
				removeChild(theend);
			}
			score=0;
			hintTimes=3;
			washTimes=2;
			currentLevel=0;
			initLevel();
		}
		private function timerHandler(event:TimerEvent):void {
			clockBar.nextFrame();
			if (clockBar.currentFrame==clockBar.totalFrames) {
				this.dispatchEvent(Game.NO_TIME);
			}
		}
		private function bingo(e:Event):void {
			bingomp3.play();
			iconLeft-=2;
			score+=10;
			if (iconLeft==0) {
				WIN();
				return;
			}
			if (! llkLevel.hintBtnClick(false)) {
				if (washTimes>0) {//有洗牌数才洗牌
					washTimes--;
					llkLevel.randomBtnClick();
					randommp3.play();
				} else {//没有洗牌数，且无法消除，输
					this.dispatchEvent(Game.NO_WASH);
					return;
				}
			}
			if (clockBar.currentFrame>8) {
				clockBar.gotoAndStop(clockBar.currentFrame-8);
			}
		}
		private function WIN():void {
			removeChild(llkLevel);
			hintBtn.removeEventListener(MouseEvent.CLICK,hintBtnClick);
			randomBtn.removeEventListener(MouseEvent.CLICK,randomBtnClick);
			llkLevel.enabled=false;
			llkTimer.stop();
			cntdownTimer.start();
		}
		private function addTime(e:TimerEvent):void {
			score+=2;
			clockBar.nextFrame();
			if (clockBar.currentFrame<250&&clockBar.currentFrame%20==0) {
				dump3.play(15);
			}
			clockBar.nextFrame();
			if (clockBar.currentFrame<250&&clockBar.currentFrame%20==0) {
				dump3.play(15);
			}
			if (clockBar.totalFrames==clockBar.currentFrame) {
				//dump3.stop();
				cntdownTimer.stop();
				if (currentLevel==9) {
					theend=new TheEnd(theendtree);
					addChild(theend);
					return;
				}
				gift=new Gift(GIFT);
				addChild(gift);
				xmasmp3=new XmasSound();
				xmasmp3.MP3.play();
			}
		}
		private function theendtree(e:Event):void{
					resetBtn.x=420;
					resetBtn.y=20;
					//removeChild(resetBtn);
					addChild(resetBtn);
			
		}
		private function GIFT(e:Event):void {
			if (gift.giftIndex>0) {//提示＋
				hintTimes+=3;
			} else {//洗牌＋
				washTimes+=3;
			}
			removeChild(gift);
			currentLevel++;
			initLevel();
		}
		private function LOSE(e:Event):void {
			switch (e.type) {
				case Game.WASH_ERR.type :
				case Game.NO_WASH.type :
					losetxt.gotoAndStop(1);
					losetxt.x=(550-losetxt.width)/2;
					losetxt.y=110;
					addChild(losetxt);
					break;
				case Game.NO_TIME.type :
					losetxt.gotoAndStop(2);
					losetxt.x=(550-losetxt.width)/2;
					losetxt.y=110;
					addChild(losetxt);
			}
			hintBtn.removeEventListener(MouseEvent.CLICK,hintBtnClick);
			randomBtn.removeEventListener(MouseEvent.CLICK,randomBtnClick);
			llkLevel.enabled=false;
			llkLevel.alpha=.14;
			llkTimer.stop();
			resetBtn.x=248;
			resetBtn.y=210;
			//removeChild(resetBtn);
			addChild(resetBtn);


		}
	}
}